//
//  TBRHomeViewC.m
//  TBRentClient
//
//  Created by tonbright on 2018/4/20.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TBRHomeViewC.h"
#import "LocationHelper.h"

#import "TBRHomeScrollCell.h"
#import "TBRModuleCell.h"

#import "NetWorkRequest.h"


#import <HMScannerController.h>
#import "BRPickerView.h"

#import "TBRRentController.h"

@interface TBRHomeViewC()<UITableViewDelegate, UITableViewDataSource,ISGLocationHelperDelegate,TBREnumModuleCellDelegate>
@property (nonatomic, strong) UITableView      *tableView;
@property (nonatomic, strong) LocationHelper   *locationHelper;

@property (nonatomic,copy)NSString *locationCity;//定位城市
@property (nonatomic,copy)NSString *locationProvince;//定位省份
@property (nonatomic,copy)NSString *locationArea;//定位区域
@property (nonatomic,copy)NSString *currentCity;//当前选择城市  默认选择上海

@property (nonatomic,strong)NSArray *imageArray;//顶部banner数组
@end

@implementation TBRHomeViewC

- (void)viewDidLoad {
    [super viewDidLoad];
    //开始定位--------
    [self startLocation];

    //设置界面-----
    [self setupUI];
    
    [self getData];
    NSLog(@"%@",Port);
}

- (void)setupUI {
    //设置导航条
    [self setupNavItem];
    
    //添加tableView
    [self setupTableView];
}

- (void)setupNavItem {
    _currentCity = @"上海市";
    self.titleText = @"用车无忧";
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:_currentCity style:UIBarButtonItemStylePlain target:nil action:nil];
    leftItem.enabled = NO;
    self.navigationItem.leftBarButtonItem = leftItem;
    
    [self ISG_NavigationBarWithBackButtonTitle:_currentCity leftImageNamed:@"定位" andAction:@selector(selCityBtnClick)];
    [self ISG_NavigationBarRightBarWithTitle:@"扫码" rightImageNamed:@"二维码" andAction:@selector(scanCode)];
}

- (void)setupTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.offset(0);
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerNib:[UINib nibWithNibName:@"TBRHomeScrollCell" bundle:nil] forCellReuseIdentifier:@"TBRHomeScrollCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TBRModuleCell" bundle:nil] forCellReuseIdentifier:@"TBRModuleCellId"];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 100;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
}


#pragma mark - 实现数据源方法
#pragma mark - 返回组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

#pragma mark - 返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

#pragma mark - 返回每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //自定义cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self getIdentifier:indexPath] forIndexPath:indexPath];
//    cell.goodsModel = self.allGoodsArray[indexPath.row];
    //设置数据
    //返回
    [self setupCell:cell indexPath:indexPath];
    return cell;
}

- (void)setupCell:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        TBRHomeScrollCell *cellScroll = (TBRHomeScrollCell *)cell;
//        cellScroll.delegate = self;
        cellScroll.imageArr = self.imageArray;
    }
    else if (indexPath.row == 1) {
        TBRModuleCell *cellModule = (TBRModuleCell *)cell;
        cellModule.delegate = self;
//        cellModule.benchModel = _benchModel;
//        cellModule.moduleArray = self.moduleWorkArray;
        
    }
}

- (NSString *)getIdentifier:(NSIndexPath *)indexPath {
    return indexPath.row == 0 ? @"TBRHomeScrollCellId" : @"TBRModuleCellId";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return AdaptH(130);
    }
    return ((4 - 1) / 2 + 1) * 97 + 30;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

// MARK: - 点击选择城市按钮点击事件--------------
- (void)selCityBtnClick {
    NSLog(@"我被点击了  现在要跳转到其他城市了");
    __weak typeof(self) weakSelf = self;
    [BRStringPickerView showStringPickerWithTitle:@"选择城市" dataSource:@[@"上海市", @"杭州市", @"北京市"] defaultSelValue:_currentCity resultBlock:^(id selectValue) {
        weakSelf.currentCity = selectValue;
        [weakSelf ISG_NavigationBarWithBackButtonTitle:_currentCity leftImageNamed:@"定位" andAction:@selector(selCityBtnClick)];
    }];
   
}

- (void)scanCode {
    NSLog(@"跳转到扫描二维码界面------------");
    NSString *cardName = @"龙飞凤舞";
    UIImage *avatar = [UIImage imageNamed:@"avatar"];
    HMScannerController *scanner = [HMScannerController scannerWithCardName:cardName avatar:avatar completion:^(NSString *stringValue) {
        //        self.scanResultLabel.text = stringValue;
        //取出返回的二维码数据   发送网络请求  绑定优惠券-------------
    }];
    [scanner setTitleColor:[UIColor whiteColor] tintColor:[UIColor greenColor]];
    [self showDetailViewController:scanner sender:nil];
}


- (void)enumModuleCellClick:(NSInteger)tag {
    NSLog(@"第几个按钮被点击了");
    TBRRentController *rentC = [[TBRRentController alloc] init];
    rentC.selectedIndex = tag + 1;//传递的值应该是多1的
    [self.navigationController pushViewController:rentC animated:YES];
}


#pragma mark -ISGLocationHelperDelegate
- (void)locationHelper:(LocationHelper *)helper successWithModel:(ISGLocationInfoModel *)model {
    NSString *province = model.province;
    NSString *city = model.city;
    if ([province containsString:@"市"]) {
        //四大直辖市的城市信息无法通过locality获得，只能通过获取省份的方法来获得（如果city为空，则可知为直辖市）
        province = [province stringByReplacingOccurrencesOfString:@"市" withString:@""];
    }
    _locationCity = city;
    _locationProvince = province;
    _locationArea = model.area;
//    [self ISG_NavigationBarRightBarWithTitle:_locationCity rightImageNamed:@"hp_locationD" andAction:@selector(selCityBtnClick)];
}

- (void)startLocation {
    _locationHelper = [[LocationHelper alloc] init];
    _locationHelper.delegate = self;
    [_locationHelper startLocation];
}


- (void)getData {
    NSArray *imageArrary = @[@"http://inte.99zmall.com/static/img/shopActivityImg/46cde6fa79d8410a8f443f50824d0984.jpg",
                             @"http://inte.99zmall.com/static/img/shopActivityImg/9396e18e1a004ec484e52daa8c51ab7f.jpg",
                             @"http://inte.99zmall.com/static/img/shopActivityImg/448c93a4948444f29ec1acd1bfdee45b.jpg"];
    self.imageArray = imageArrary;
    [self.tableView reloadData];
}

@end
